#include "clkTexture.h"

clkTexture::clkTexture()
{
	m_hasTexture = false;
	m_isHover = false;
	m_isClick = false;
	m_isHeld  = false;
}

clkTexture::~clkTexture()
{

}

void clkTexture::initTexture(
	char* a_texturePath,
	int   a_cen,
	V2DF  a_trn,
	V2DF  a_scl,
	float a_rot)
{
	m_d3d->createTexture2D(&m_texture,a_texturePath);

	float fw = (float)m_texture.m_texWidth;
	float fh = (float)m_texture.m_texHeight;
	float hw = fw/2.0f;
	float hh = fh/2.0f;

	switch(a_cen)
	{
	case TEX_C:  m_cen.set(-hw,-hh); break;
	case TEX_TC: m_cen.set(-hw,0);   break;
	case TEX_BC: m_cen.set(-hw,-fw); break;
	case TEX_LC: m_cen.set(0,-hh);   break;
	case TEX_RC: m_cen.set(-fw,-hh); break;
	case TEX_TL: m_cen.set(0,0);     break;
	case TEX_TR: m_cen.set(-fw,0);   break;
	case TEX_BL: m_cen.set(0,-fh);   break;
	case TEX_BR: m_cen.set(-fw,-fh); break;
	}

	m_trn = a_trn;
	m_scl = a_scl;
	m_rot = a_rot;

	if(m_texture.m_texId != -1)
		m_hasTexture = true;
}

void clkTexture::input()
{
	m_din->mousePosition(&m_cur.x,&m_cur.y);
}

void clkTexture::update()
{
	float fw = (float)m_texture.m_texWidth;
	float fh = (float)m_texture.m_texHeight;

	m_min.set(0,0);
	m_max.set(fw,fh);
	m_min += m_trn + m_cen;
	m_max += m_trn + m_cen;

	if(pointInAABB(m_cur,m_min,m_max))
	{
		m_isHover = true;
		if(m_din->mousePress(BTN_LEFT))
			m_isClick = true;
		else
			m_isClick = false;

		if(m_din->mouseInput(BTN_LEFT))
			m_isHeld = true;
		else
			m_isHeld = false;
	}
	else
	{
		m_isHover = false;
		m_isClick = false;
		m_isHeld  = false;
	}

	if(m_hasTexture)
		m_d3d->updateTransform(&m_texture,m_cen,m_trn,m_scl,m_rot);
}

void clkTexture::draw()
{
	if(m_hasTexture)
		m_d3d->drawTexture2D(&m_texture);
}